/*
 * @(#)MtmWorkflowProgressSystemObject.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import oracle.sql.BLOB;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject;

/**
 * MtmWorkflowProgressSystemObjectDAObject.java
 *
 * This class is the data access bean for table "MTM_WF_PROGRESS_SYSTEM_OBJECT".
 *
 * @author Rollo Chan
 * @company DCIVision Limited
 * @creation date 26/02/2004
 * @version $Revision: 1.17 $
*/

public class MtmWorkflowProgressSystemObjectDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.17 $";

  public static final String TABLE_NAME = "MTM_WF_PROGRESS_SYSTEM_OBJECT";

  public MtmWorkflowProgressSystemObjectDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("TRACK_ID");
    this.vecDBColumn.add("WORKFLOW_PROGRESS_ID");
    this.vecDBColumn.add("WORKFLOW_ACTION_ID");
    this.vecDBColumn.add("PARAMETER_OBJECT");
    this.vecDBColumn.add("BEAN_CLASS_NAME");
    this.vecDBColumn.add("OBJECT_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          return(tmpMtmWorkflowProgressSystemObject);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          tmpMtmWorkflowProgressSystemObject.setRecordCount(totalNumOfRecord);
          tmpMtmWorkflowProgressSystemObject.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmWorkflowProgressSystemObject);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          result.add(tmpMtmWorkflowProgressSystemObject);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)((MtmWorkflowProgressSystemObject)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        if (DataSourceFactory.DB_ORACLE!=DataSourceFactory.getDatabaseType()) {
          sqlStat.append("INSERT ");
          sqlStat.append("INTO   MTM_WF_PROGRESS_SYSTEM_OBJECT(ID, TRACK_ID, WORKFLOW_PROGRESS_ID, WORKFLOW_ACTION_ID, PARAMETER_OBJECT, BEAN_CLASS_NAME, OBJECT_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
          sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, nextID);
          setPrepareStatement(preStat, 2, tmpMtmWorkflowProgressSystemObject.getTrackID());
          setPrepareStatement(preStat, 3, tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID());
          setPrepareStatement(preStat, 4, tmpMtmWorkflowProgressSystemObject.getWorkflowActionID());
          setPrepareStatement(preStat, 5, tmpMtmWorkflowProgressSystemObject.getParameterObject());
          setPrepareStatement(preStat, 6, tmpMtmWorkflowProgressSystemObject.getBeanClassName());
          setPrepareStatement(preStat, 7, tmpMtmWorkflowProgressSystemObject.getObjectID());
          setPrepareStatement(preStat, 8, GlobalConstant.RECORD_STATUS_ACTIVE);
          setPrepareStatement(preStat, 9, new Integer(0));
          setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 11, currTime);
          setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 13, currTime);
          preStat.executeUpdate();
        } else {
          sqlStat.append("INSERT ");
          sqlStat.append("INTO   MTM_WF_PROGRESS_SYSTEM_OBJECT(ID, TRACK_ID, WORKFLOW_PROGRESS_ID, WORKFLOW_ACTION_ID, PARAMETER_OBJECT, BEAN_CLASS_NAME, OBJECT_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
          sqlStat.append("VALUES (?, ?, ?, ?, EMPTY_BLOB(), ?, ?, ?, ?, ?, ?, ?, ?) ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, nextID);
          setPrepareStatement(preStat, 2, tmpMtmWorkflowProgressSystemObject.getTrackID());
          setPrepareStatement(preStat, 3, tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID());
          setPrepareStatement(preStat, 4, tmpMtmWorkflowProgressSystemObject.getWorkflowActionID());
          setPrepareStatement(preStat, 5, tmpMtmWorkflowProgressSystemObject.getBeanClassName());
          setPrepareStatement(preStat, 6, tmpMtmWorkflowProgressSystemObject.getObjectID());
          setPrepareStatement(preStat, 7, GlobalConstant.RECORD_STATUS_ACTIVE);
          setPrepareStatement(preStat, 8, new Integer(0));
          setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 10, currTime);
          setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 12, currTime);
          preStat.executeUpdate();
          preStat.close();

          ResultSet rs = null;
          BLOB blob = null;
          sqlStat = new StringBuffer();
          sqlStat.append("SELECT  PARAMETER_OBJECT ");
          sqlStat.append("FROM  MTM_WF_PROGRESS_SYSTEM_OBJECT ");
          sqlStat.append("WHERE ID = " + nextID);
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          rs = preStat.executeQuery();
          if (rs.next()) {
            blob = (BLOB)rs.getBlob("PARAMETER_OBJECT");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
              ObjectOutputStream out = new ObjectOutputStream(baos);
              out.writeObject(tmpMtmWorkflowProgressSystemObject.getParameterObject());
              out.flush();
            } catch (java.io.IOException ioe) {
              log.error("Cannot write object stream", ioe);
              new java.sql.SQLException("Cannot write object stream");
            }
            blob.putBytes(1, baos.toByteArray());

            sqlStat = new StringBuffer();
            sqlStat.append("UPDATE  MTM_WF_PROGRESS_SYSTEM_OBJECT ");
            sqlStat.append("SET PARAMETER_OBJECT = ? ");
            sqlStat.append("WHERE ID = ? ");
            PreparedStatement preStat2 = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            preStat2.setBlob(1, blob);
            preStat2.setInt(2, nextID.intValue());
            preStat2.executeUpdate();
            try { preStat2.close(); } catch (Exception ignore) {} finally {preStat2 = null; }
          }
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        }
        tmpMtmWorkflowProgressSystemObject.setID(nextID);
        tmpMtmWorkflowProgressSystemObject.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowProgressSystemObject.setCreateDate(currTime);
        tmpMtmWorkflowProgressSystemObject.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowProgressSystemObject.setUpdateDate(currTime);
        tmpMtmWorkflowProgressSystemObject.setUpdateCount(new Integer(0));
        tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
        tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
        return(tmpMtmWorkflowProgressSystemObject);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)((MtmWorkflowProgressSystemObject)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        if (DataSourceFactory.DB_ORACLE!=DataSourceFactory.getDatabaseType()) {
log.debug("Normal update of blob");
          sqlStat.append("UPDATE MTM_WF_PROGRESS_SYSTEM_OBJECT ");
          sqlStat.append("SET  TRACK_ID=?, WORKFLOW_PROGRESS_ID=?, WORKFLOW_ACTION_ID=?, PARAMETER_OBJECT=?, BEAN_CLASS_NAME=?, OBJECT_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpMtmWorkflowProgressSystemObject.getTrackID());
          setPrepareStatement(preStat, 2, tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID());
          setPrepareStatement(preStat, 3, tmpMtmWorkflowProgressSystemObject.getWorkflowActionID());
          setPrepareStatement(preStat, 4, tmpMtmWorkflowProgressSystemObject.getParameterObject());
          setPrepareStatement(preStat, 5, tmpMtmWorkflowProgressSystemObject.getBeanClassName());
          setPrepareStatement(preStat, 6, tmpMtmWorkflowProgressSystemObject.getObjectID());
          setPrepareStatement(preStat, 7, new Integer(tmpMtmWorkflowProgressSystemObject.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 9, currTime);
          setPrepareStatement(preStat, 10, tmpMtmWorkflowProgressSystemObject.getID());
          setPrepareStatement(preStat, 11, tmpMtmWorkflowProgressSystemObject.getUpdateCount());
          updateCnt = preStat.executeUpdate();
        } else {
          sqlStat.append("UPDATE MTM_WF_PROGRESS_SYSTEM_OBJECT ");
          sqlStat.append("SET  TRACK_ID=?, WORKFLOW_PROGRESS_ID=?, WORKFLOW_ACTION_ID=?, PARAMETER_OBJECT=EMPTY_BLOB(), BEAN_CLASS_NAME=?, OBJECT_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpMtmWorkflowProgressSystemObject.getTrackID());
          setPrepareStatement(preStat, 2, tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID());
          setPrepareStatement(preStat, 3, tmpMtmWorkflowProgressSystemObject.getWorkflowActionID());
          setPrepareStatement(preStat, 4, tmpMtmWorkflowProgressSystemObject.getBeanClassName());
          setPrepareStatement(preStat, 5, tmpMtmWorkflowProgressSystemObject.getObjectID());
          setPrepareStatement(preStat, 6, new Integer(tmpMtmWorkflowProgressSystemObject.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 7, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 8, currTime);
          setPrepareStatement(preStat, 9, tmpMtmWorkflowProgressSystemObject.getID());
          setPrepareStatement(preStat, 10, tmpMtmWorkflowProgressSystemObject.getUpdateCount());
          updateCnt = preStat.executeUpdate();
          if(updateCnt>0){
log.debug("Begin special handling for oracle update blob");
            ResultSet rs = null;
            BLOB blob = null;
            sqlStat = new StringBuffer();
            sqlStat.append("SELECT  PARAMETER_OBJECT ");
            sqlStat.append("FROM  MTM_WF_PROGRESS_SYSTEM_OBJECT ");
            sqlStat.append("WHERE ID = " + tmpMtmWorkflowProgressSystemObject.getID());
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = preStat.executeQuery();
            if (rs.next()) {
              blob = (BLOB)rs.getBlob("PARAMETER_OBJECT");
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              try {
                ObjectOutputStream out = new ObjectOutputStream(baos);
                out.writeObject(tmpMtmWorkflowProgressSystemObject.getParameterObject());
                out.flush();
log.debug("Finished writing blob");
              } catch (java.io.IOException ioe) {
                log.error("Cannot write object stream", ioe);
                new java.sql.SQLException("Cannot write object stream");
              }
              blob.putBytes(1, baos.toByteArray());

              sqlStat = new StringBuffer();
              sqlStat.append("UPDATE  MTM_WF_PROGRESS_SYSTEM_OBJECT ");
              sqlStat.append("SET PARAMETER_OBJECT = ? ");
              sqlStat.append("WHERE ID = ? ");
              PreparedStatement preStat2 = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
              preStat2.setBlob(1, blob);
              preStat2.setInt(2, tmpMtmWorkflowProgressSystemObject.getID().intValue());
              preStat2.executeUpdate();
log.debug("Finished updating parameter_object");
              try { preStat2.close(); } catch (Exception ignore) {} finally {preStat2 = null; }
            }
            try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          }
        }

        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(currTime);
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(new Integer(tmpMtmWorkflowProgressSystemObject.getUpdateCount().intValue() + 1));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          return(tmpMtmWorkflowProgressSystemObject);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)((MtmWorkflowProgressSystemObject)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowProgressSystemObject.getID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowProgressSystemObject.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmWorkflowProgressSystemObject);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)this.oldValue;

    if (tmpMtmWorkflowProgressSystemObject != null) {
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getTrackID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getWorkflowActionID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getParameterObject()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getBeanClassName()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getObjectID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdateDate()));
    }

    tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)obj;
    if (tmpMtmWorkflowProgressSystemObject != null) {
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getTrackID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getWorkflowProgressID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getWorkflowActionID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getParameterObject()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getBeanClassName()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getObjectID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowProgressSystemObject.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  /**
   * Gets the system object for a given workflow track ,workflow progress and workflow action
   * 
   * @param trackID The workflow track ID
   * @param workflowProgressID The workflow progress ID
   * @param workflowActionID The workflow action ID
   * @return The system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized AbstractBaseObject getObjectByTrackIDWorkflowProgressIDWorkflowActionID(Integer trackID, Integer workflowProgressID, Integer workflowActionID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        if (!Utility.isEmpty(trackID)) {
          sqlStat.append("AND A.TRACK_ID = ? ");
        }
        if (!Utility.isEmpty(workflowProgressID)) {
          sqlStat.append("AND A.WORKFLOW_PROGRESS_ID = ? ");
        }
        if (!Utility.isEmpty(workflowActionID)) {
          sqlStat.append("AND A.WORKFLOW_ACTION_ID = ? ");
        }
        sqlStat.append("ORDER BY A.ID ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i=1;
        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (!Utility.isEmpty(trackID)) {
          this.setPrepareStatement(preStat, i++, trackID);
        }
        if (!Utility.isEmpty(workflowProgressID)) {
          this.setPrepareStatement(preStat, i++, workflowProgressID);
        }
        if (!Utility.isEmpty(workflowActionID)) {
          this.setPrepareStatement(preStat, i++, workflowActionID);
        }
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          return (tmpMtmWorkflowProgressSystemObject);
        } else {
          return (null);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets the system object for a given workflow progress
   * 
   * @param workflowProgressID The workflow progress ID
   * @return The system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized AbstractBaseObject getObjectByWorkflowProgressID(Integer workflowProgressID) throws ApplicationException {
    return this.getObjectByTrackIDWorkflowProgressIDWorkflowActionID(null, workflowProgressID, null);
  }
  /**
   * Gets the system object for a given workflow action
   * 
   * @param workflowActionID The workflow action ID
   * @return The system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized AbstractBaseObject getObjectByWorkflowActionID(Integer workflowActionID) throws ApplicationException {
    return this.getObjectByTrackIDWorkflowProgressIDWorkflowActionID(null, null, workflowActionID);
  }
  /**
   * Gets a list of system object by class name and object id
   * 
   * @param beanClassName The fully qualified class name
   * @param objID The object ID
   * @return List of system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized List getListByBeanClassNameObjectID(String beanClassName, Integer objID) throws ApplicationException {
    return this.getListByBeanClassNameObjectIDTrackID(beanClassName, objID, null);
  }
  /**
   * Gets a list of system object by class name and track id
   * @param beanClassName
   * @param trackID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByBeanClassNameTrackID(String beanClassName, Integer trackID) throws ApplicationException {
    return this.getListByBeanClassNameObjectIDTrackID(beanClassName, null, trackID);
  }
  
  /**
   * Gets a list of system object by class name and object id and workflow track id
   * @param beanClassName
   * @param objID
   * @param WorkflowTrackID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByBeanClassNameObjectIDTrackID(String beanClassName, Integer objID, Integer WorkflowTrackID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.BEAN_CLASS_NAME = ?  AND A.RECORD_STATUS = ? ");
        if (!Utility.isEmpty(objID)) {
          sqlStat.append("  AND A.OBJECT_ID = ? ");
        }
        if (!Utility.isEmpty(WorkflowTrackID)) {
          sqlStat.append("  AND A.TRACK_ID = ? ");
        }
        sqlStat.append("ORDER BY A.TRACK_ID ASC" );
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i=1;
        this.setPrepareStatement(preStat, i++, beanClassName);
        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (!Utility.isEmpty(objID)) {
          this.setPrepareStatement(preStat, i++, objID);
        }
        if (!Utility.isEmpty(WorkflowTrackID)) {
          this.setPrepareStatement(preStat, i++, WorkflowTrackID);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          result.add(tmpMtmWorkflowProgressSystemObject);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes the given system object
   * 
   * @param obj The system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized void deleteByClassNameObjectID(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = (MtmWorkflowProgressSystemObject)((MtmWorkflowProgressSystemObject)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT ");
        sqlStat.append("WHERE  ID=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowProgressSystemObject.getID());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          //throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Gets the system object of a given workflow track,workflow porgress and system object
   * 
   * @param trackID The workflow track ID
   * @param workflowProgressID The workflow progress ID
   * @param objectID The system object ID
   * @return The system object(MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject
   */
  public synchronized AbstractBaseObject getObjectByTrackIDWorkflowProgressIDObjectID(Integer trackID, Integer workflowProgressID, Integer objectID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        if (!Utility.isEmpty(trackID)) {
          sqlStat.append("AND A.TRACK_ID = ? ");
        }
        if (!Utility.isEmpty(workflowProgressID)) {
          sqlStat.append("AND A.WORKFLOW_PROGRESS_ID = ? ");
        }
        if (!Utility.isEmpty(objectID)) {
          sqlStat.append("AND A.OBJECT_ID = ? ");
        }
        sqlStat.append("ORDER BY A.ID ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i=1;
        this.setPrepareStatement(preStat, i++, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (!Utility.isEmpty(trackID)) {
          this.setPrepareStatement(preStat, i++, trackID);
        }
        if (!Utility.isEmpty(workflowProgressID)) {
          this.setPrepareStatement(preStat, i++, workflowProgressID);
        }
        if (!Utility.isEmpty(objectID)) {
          this.setPrepareStatement(preStat, i++, objectID);
        }
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          return (tmpMtmWorkflowProgressSystemObject);
        } else {
          return (null);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets the system object of a given workflow track and system object
   * 
   * @param trackID The workflow track ID
   * @param objectID The system object ID
   * @return The system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject
   */
  public synchronized AbstractBaseObject getObjectByTrackIDObjectID(Integer trackID, Integer objectID) throws ApplicationException {
    return this.getObjectByTrackIDWorkflowProgressIDObjectID(trackID, null, objectID);
  }

  /**
   * Gets a list of system object by workflow  track for given workflow track
   * 
   * @param trackID The workflow track ID
   * @return List of system object (MtmWorkflowProgressSystemObject)
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject 
   */
  public synchronized List getListByTrackID(Integer trackID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TRACK_ID, A.WORKFLOW_PROGRESS_ID, A.WORKFLOW_ACTION_ID, A.PARAMETER_OBJECT, A.BEAN_CLASS_NAME, A.OBJECT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_PROGRESS_SYSTEM_OBJECT A ");
        sqlStat.append("WHERE  A.TRACK_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.TRACK_ID ASC" );
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, trackID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowProgressSystemObject tmpMtmWorkflowProgressSystemObject = new MtmWorkflowProgressSystemObject();
          tmpMtmWorkflowProgressSystemObject.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowProgressSystemObject.setTrackID(getResultSetInteger(rs, "TRACK_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowProgressSystemObject.setWorkflowActionID(getResultSetInteger(rs, "WORKFLOW_ACTION_ID"));
          tmpMtmWorkflowProgressSystemObject.setParameterObject(getResultSetObject(rs, "PARAMETER_OBJECT"));
          tmpMtmWorkflowProgressSystemObject.setBeanClassName(getResultSetString(rs, "BEAN_CLASS_NAME"));
          tmpMtmWorkflowProgressSystemObject.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpMtmWorkflowProgressSystemObject.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowProgressSystemObject.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowProgressSystemObject.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowProgressSystemObject.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowProgressSystemObject.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowProgressSystemObject.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getCreatorID()));
          tmpMtmWorkflowProgressSystemObject.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowProgressSystemObject.getUpdaterID()));
          result.add(tmpMtmWorkflowProgressSystemObject);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
}
